// Schema主要用于 "定义" MongoDB中集合Collection里文档document的结构
// 每个schema会 "映射" 到mongodb中的一个collection，schema "不具备" 操作数据库的能力
// 再由Model模型对数据库进行操作

/*define的数据类型
String      字符串
Number      数字
Date        日期
Buffer      二进制
Boolean     布尔值
Mixed       混合类型
ObjectId    对象ID
Array       数组*/


const mongoose = require('mongoose');//导入Model 构造器
const Schema = mongoose.Schema;//构造函数

// 实例化的时候数据尽量写全(内部也要写 ---  "错误:内部不要写");如没写的,不能对其操作。 
// 不过后期需要字段可以再加: MySchema.add({ name: 'string', color: 'string', price: 'number' });
// 声明字段类型有两种方法，一种是首字母大写的字段类型，另一种是引号包含的小写字段类型   String==='string'

let movies_schema = new Schema({
    filmId: Number,
    name: String,
    poster: String,
    actors: Array,
    director: String,
    category: String,
    synopsis: String,
    filmType: Object,
    nation: String,
    language: String,
    videoId: String,
    premiereAt: Number,
    timeType: Number,
    runtime: Number,
    grade: Number,
    // item: {
    //     name: String,
    //     type: Number
    // },
    item: Object,
    isPresale: Boolean,
    isSale: Boolean
});

function zhushiModel() {
    // Model 模型: 
    // 第一个参数是模型/文档名称(一个参数和其返回值设置为相同的值，否则会出现不可预知的结果!!!!!!);第二个参数为Schema的实例化对象  
    // Model是由Schema编译而成的假想（fancy）构造器，具有抽象属性和行为
    // Model的每一个实例（instance）就是一个document，document可以保存到数据库和对数据库进行操作
    // model是由schema生成的模型，可以对数据库的操作

    /*// 实例方法: Model的实例是document，内置实例方法有很多，如 save，可以通过Schema对象的methods属性给实例自定义扩展方法
    schema.methods.findSimilarSizes = function(cb){
        return this.model('MyModel').find({size:this.size},cb);
    }*/

    /*// 静态方法: 通过Schema对象的statics属性给 Model 添加静态方法
    schema.statics.findByName = function(name,cb){
        return this.find({name: new RegExp(name,'i')},cb);
    }*/

    /*// 两种方法的使用区别: 导入之后使用的
    // statics
    Article.staticFunc ()
    
    //methods
    const article = new Article(arguments)
    article.methodFunc()
    */
}

exports.Movie = mongoose.model('Movie', movies_schema);
// Mongoose会将集合名称设置为模型名称的小写版: (在这儿的)大小写组合(在数据库中)都会变成小写的
// user => users //在这儿没有复数;在数据库中对应着他的复数。若结尾是数字就没这个烦恼


let userinfo0_schema = new Schema({
    username: String,
    password: String,
    mobile: Number,
    uid: Number,
    time: String,
});
exports.Userinfo0 = mongoose.model('userinfo0', userinfo0_schema);
//  bug: can not read property of 'images' (item.images.large)多层的时候也许前面的images段不在,发现是schema没写images
let uid0_schema = new Schema({
    username: String,
    id: Number,
});
exports.Uid0 = mongoose.model('uid0', uid0_schema);

let comment_schema = new Schema({
    username: String,//session

    m_uid: Number,//uid0

    title: String,//ajax
    content: String,
    filmId: Number,
    name: String,

    time: String,
});
exports.Comment = mongoose.model('comment', comment_schema);


let vuer_schema = new Schema({
    username: String,//session
    vid: Number,//uid0
    mobile: String,//ajax
    password: String,
    dbpwd: String,
    nickname: String,
    time: String,
    avatar: String
});
exports.Vuer = mongoose.model('vuer', vuer_schema);

let good_schema = new Schema({
    goods_id: Number,
    goods_name: String,
    short_name: String,
    thumb_url: String,
    hd_thumb_url: String,
    image_url: String,
    sales: Number,
    cnt: Number,
    group_price: Number,
    normal_price: Number,
    market_price: Number,
    event_type: Number,
    customer_num: Number,
    mall_id: Number,
    group: Object,
    sales_tip: String,
    link_url: String,
    hd_url: String,
    quantity: Number,
    activity_type: Number,
    activity_quantity: Number,
    activity_sold_quantity: Number,
    activity_start_time: Number,
    activity_end_time: Number,
    cat_id1: Number,
    goods_type: Number,
    scores: Object,
    group_price_origin: Number,
    deposit_price: Number

});
exports.Good = mongoose.model('good', good_schema);

// sku模板
let goodlist_schema = new Schema({
    _id: String,
    goods_id: String,
    goods_type: String,
    quota: Number,
    quota_used: Number,
    start_sale_num: Number,
    goods_info: Object,
    sku: Object,
    properties: Array,
    initialSku: Object
});
exports.Goodlist = mongoose.model('goodlist', goodlist_schema);

let cartinfo_schema = new Schema({
    _id: String,
    goodsId: String,
    selectedId: Number,
    selectedNum: Number,
    selectedSkuComb: Object,
    messages: Object,
    cartMessages: Object,
    user: String,
    s1: String,
    s2: String,
    goods_info: Object
});
exports.Cartinfo = mongoose.model('cartinfo', cartinfo_schema);

let addressinfo_schema = new Schema({
  name: String,
  tel: String,
  country: String,
  province: String,
  city: String,
  county: String,
  areaCode: String,
  postalCode: String,
  addressDetail: String,
  isDefault: Boolean,
  id: String,
  address: String,
  user: String
});
exports.Addressinfo = mongoose.model('addressinfo', addressinfo_schema);


let cityinfo_schema = new Schema({
  cityId: Number,
  name: String,
  pinyin: String,
  isHot: Number,
  id: String,
  cityEn: String,
  cityZh: String,
  provinceEn: String,
  provinceZh: String,
  leaderEn: String,
  leaderZh: String,
  lat: String,
  lon: String
});
exports.Cityinfo = mongoose.model('cityinfo', cityinfo_schema);

// 如果遇到加入数据的数据不一样,可以来看看schema配了没 
